package com.dongdongshop.controller;

import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.ContentCategoryService;
import com.dongdongshop.service.ContentService;
import com.dongdongsop.data.AjaxResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {

    @DubboReference
    private ContentService contentService;

    @DubboReference
    private ContentCategoryService categoryService;

    //跳转广告管理
    @RequestMapping("toContent")
    public String toContent(){
        return "admin/content";
    }

    //查询广告管理数据
    @RequestMapping("contentPageInfo")
    @ResponseBody
    public List<TbContent> contentPageInfo(){
        List<TbContent> contentList = contentService.getContentPageInfo();
        return contentList;
    }

    //跳转广告管理添加页面
    @RequestMapping("toContentAdd")
    public String toContentAdd(Model model){
        List<TbContentCategory> categoryList = categoryService.findAll();
        model.addAttribute("categoryList",categoryList);
        return "content/add";
    }

    //广告管理添加
    @RequestMapping("contentAdd")
    @ResponseBody
    public AjaxResult contentAdd(TbContent content){
        try {
            int i = contentService.contentAdd(content);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //跳转广告管理修改页面
    @RequestMapping("toContentUpdate")
    public String toContentUpdate(@RequestParam("id") Long id, Model model){
        TbContent content = contentService.toContentUpdate(id);
        List<TbContentCategory> categoryList = categoryService.findAll();
        model.addAttribute("content",content);
        model.addAttribute("categoryList",categoryList);
        return "content/update";
    }

    //广告管理修改
    @RequestMapping("contentUpdate")
    @ResponseBody
    public AjaxResult contentUpdate(TbContent content){
        try {
            int i = contentService.contentUpdate(content);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //广告管理删除
    @RequestMapping("deleteId")
    @ResponseBody
    public AjaxResult deleteId(Long id){
        try {
            int i = contentService.deleteId(id);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //广告管理审核
    @RequestMapping("contentAudit")
    @ResponseBody
    public AjaxResult contentAudit(TbContent content){
        try {
            int i = contentService.contentAudit(content);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
